<template>
  <card>
    <h4 slot="header" class="card-title">管理员信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="姓名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="工号">
        <el-input v-model="form.number" disabled></el-input>
      </el-form-item>
      <el-form-item label="原始密码" prop="pass">
        <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input type="password" v-model="form.new_password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPass">
        <el-input type="password" v-model="form.new_password_confirmation" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="权限">
        <el-select v-model="form.role" placeholder="请选择权限" disabled>
          <el-option label="超级管理员" value="shanghai"></el-option>
          <el-option label="业主管理员" value="beijing"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">保存</el-button>
      </el-form-item>
    </el-form>
  </card>
</template>
<script>
export default {
  data() {
    return {
      form: {
        password: "",
        new_password: "",
        new_password_confirmation: "",
        username: "",
        role: "",
        number: "",
        date1: "",
        sex: ""
      }
    };
  },
  created: function() {
    this.getMe();
  },
  methods: {
    getMe() {
      this.$ajax({
        method: "get",
        url: "/admin/info"
      })
        .then(res => {
          this.form = res.data.data[0];
        })
        .catch(error => {
          alert(error);
          console.log(error);
        });
    },
    onSubmit(formName) {
      if(this.form.new_password!=''){
        if(this.form.new_password_confirmation===''){
           this.$message({
              type: "info",
              message: "请输入再次确认密码"
            });
        }else if(this.form.new_password_confirmation!==this.form.new_password){
          this.$message({
              type: "info",
              message: "两次输入密码不一致"
            });
        }else if(this.password===''){
          this.$message({
              type: "info",
              message: "请输入原密码"
            });
        }
      }
      this.$ajax({
        method: "put",
        url: "/admin/info",
        withCredentials: true,
        data: this.form
      })
        .then(res => {
          if (res.data.error_no === 1) {
            this.$message({
              type: "success",
              message: "修改成功!"
            });
          } else {
            this.$message({
              type: "info",
              message: res.data.error_msg
            });
          }
        })
        .catch(error => {
          this.$message({
            type: "info",
            message: "修改失败!"
          });
          console.log(error);
        });
    }
  }
};
</script>